package com.nfts.imagepicker.view;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.nfts.imagepicker.R;
import com.nfts.imagepicker.bean.FolderBean;
import com.nfts.imagepicker.util.ImageLoader;

import java.util.List;

/**
 * Created by 9527 on 2016/1/3.
 */
public class ListImageDirPopupWindow extends PopupWindow {
    private int mWidth;
    private int mHeight;
    private View mConvertView;
    private ListView mListView;
    private List<FolderBean>mDatas;

    public interface OnDirSelectedListener{
        void onSelected(FolderBean folderBean);
    }

    public OnDirSelectedListener mListener;

    public void setOnDirSelectedListener(OnDirSelectedListener mListener){
        this.mListener = mListener;

    }

    public ListImageDirPopupWindow(Context context,List<FolderBean>datas){
        calWidthAndHeight(context);
mConvertView = LayoutInflater.from(context).inflate(R.layout.popup_main,null);
        mDatas = datas;
        setContentView(mConvertView);
setWidth(mWidth);
        setHeight(mHeight);
        setFocusable(true);
        setTouchable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new BitmapDrawable());
        setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction()==MotionEvent.ACTION_OUTSIDE){
                    dismiss();
                    return true;
                }
                return false;
            }
        });
       initViews(context);
        initEvent();

    }

    private void initEvent() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener!=null){
                    FolderBean fb = mDatas.get(position);
                    mListener.onSelected(fb);
                }
            }
        });


    }

    private void initViews(Context context) {
        mListView = (ListView) mConvertView.findViewById(R.id.listview_popup_main);
        mListView.setAdapter(new ListDirAdapter(context,mDatas));
    }

    private void calWidthAndHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
setAnimationStyle(R.style.anim_popup_dir);
        mWidth = outMetrics.widthPixels;
        mHeight = (int) (outMetrics.heightPixels*0.8);

    }

    private class ListDirAdapter extends ArrayAdapter<FolderBean>{
private LayoutInflater mInflater;
        private List<FolderBean>mDatas;

        public ListDirAdapter(Context context, List<FolderBean> objects) {
            super(context, 0, objects);
            mInflater = LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = null;
            if (convertView==null){
                    viewHolder = new ViewHolder();
                convertView = mInflater.inflate(R.layout.item_popup,parent,false);
                viewHolder.mImg = (ImageView) convertView.findViewById(R.id.image_iv_item_popup);
                viewHolder.mDirName = (TextView) convertView.findViewById(R.id.name_tv_item_popup);
                viewHolder.mDirCount = (TextView) convertView.findViewById(R.id.count_tv_item_popup);
                convertView.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            FolderBean bean = getItem(position);
            viewHolder.mImg.setImageResource(R.drawable.pictures_no);
            ImageLoader.getInstance().loadImage(bean.getFirstImgPath(),viewHolder.mImg);
viewHolder.mDirCount.setText(bean.getCount()+"");
            viewHolder.mDirName.setText(bean.getName());

            return convertView;
        }

        private class ViewHolder{
            ImageView mImg;
            TextView mDirName;
            TextView mDirCount;
        }
    }

}
